package com.phil.xml.parse;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.phi.tuangou.bean.CityBean;

public class LashouCityHandler extends DefaultHandler {

	List<CityBean> cityList;
	CityBean cityBean = null;
	String elementName = null;

	public LashouCityHandler() {
		cityList = new ArrayList<CityBean>();
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();

		System.out.println("*******begin parse docments*******");
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (elementName.equals("cityid")) {
			String cityId = new String(ch, start, length);
			cityBean.setCityId(cityId);
			

		} else if (elementName.equals("cityname")) {
			String cityName = new String(ch, start, length);
			cityName = cityName.trim();
			if (cityName != null && !cityName.equals("")) {
				cityBean.setCityName(cityName);
				
			}
		}
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("*******end parse docments*******");
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		if (qName.equals("city")) {
			cityBean = new CityBean();
			cityList.add(cityBean);
		} else {
			
			elementName = qName;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
//		if(elementName.equals("city")){
//			cityList.add(cityBean);
//		}

	}

	public List<CityBean> getList() {
		return cityList;
	}

}
